package com.beyondsoft.giinii.brie.jni;
import java.io.File;
import java.util.ArrayList;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;


public class MainDialog extends Activity 
{
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mdialog);
        
        Intent intent = getIntent();
        mInputpath = intent.getStringExtra("InputPath");
        mOutputpath = intent.getStringExtra("OutputPath");
        
		File inputfile = new File(mInputpath);
        mFilelist = inputfile.list();
        
        if(0 < mFilelist.length)
        {
            for(int i=0; i<mFilelist.length; ++i)
            	mFilelist[i] = inputfile + "/" + mFilelist[i];
            
            GridView g = (GridView)findViewById(R.id.gridView1);
            g.setAdapter(new ImageAdapter(this));

            //LinerLayout 2, two buttons
            start = (Button) findViewById(R.id.button1);
            checkResult = (Button) findViewById(R.id.button2);
            
            start.setOnClickListener(StartFacesort);
            checkResult.setOnClickListener(ListResultFile);
            
            mDialog = onCreateDialog(0);
            mHandler = new Handler() {
        		public void handleMessage(Message msg) {
        			switch (msg.what) {
        			case 1000:
        				mDialog.show();
        				break;
        			case 2000:
        				mFacesortThread.interrupt();
        				mDialog.hide();
        				break;
        			}
        			super.handleMessage(msg);
        		}
        	};
        }
    }
    protected Dialog onCreateDialog(int id) 
    {
    	ProgressDialog dialog = new ProgressDialog(this);
    	dialog.setTitle("Face Sort Demo");
    	dialog.setMessage("Please wait while running...");
    	dialog.setIndeterminate(true);
    	dialog.setCancelable(false);
    	return dialog;
    }
    
    private OnClickListener StartFacesort = new OnClickListener() 
    {
    	public void onClick(View v) 
        {
            mFacesortThread = new Thread(){
            	@Override
            	public void run(){
        			Message showMsg = new Message();
        			showMsg.what = 1000;
        			mHandler.sendMessage(showMsg);

        			facesortInfo[] info = FacesortJniInterface.faceSort(mInputpath, mOutputpath, (byte)1);
            		//facesortInfo[] info = getTestData();
            		if(null != info)
            		{
            			int loc = info[0].facePath.lastIndexOf('/');
            			path = info[0].facePath.substring(0, loc);
            			
            			if(0 < info.length)
            			{
            				resultlist.clear();
            				clslist.clear();
            				for(int i=0; i<info.length; ++i)
            				{
            					resultlist.add(info[i].facePath);
            					clslist.add(info[i].classId);
            				}
            			}
            		}
            		Message msgHide = new Message();
            		msgHide.what = 2000;
        			mHandler.sendMessage(msgHide);
            	}
            };
    		mFacesortThread.start();
        }
    };
    private OnClickListener ListResultFile = new OnClickListener() 
    {
    	public void onClick(View v) 
        {
			Intent iten = new Intent(MainDialog.this, ListResult.class);
			iten.putExtra("pathName", path);
			iten.putExtra("fileList", resultlist);
			iten.putExtra("clsList", clslist);
			
            startActivity(iten);
        }
    };
	private facesortInfo[] getTestData()
	{
		facesortInfo[] infoobj = new facesortInfo[5];

		infoobj[0] = new facesortInfo();
		infoobj[0].facePath = "/sdcard/facesort/faces/100/images.jpg";
		infoobj[0].classId = 3;
		infoobj[0].photoName = "/sdcard/facesort/faces/100/images.jpg";
		infoobj[0].isACK = 0;
		
		infoobj[1] = new facesortInfo();
		infoobj[1].facePath = "/sdcard/facesort/faces/100/images000.jpg";
		infoobj[1].classId = 3;
		infoobj[1].photoName = "/sdcard/facesort/faces/100/images000.jpg";
		infoobj[1].isACK = 0;
		
		infoobj[2] = new facesortInfo();
		infoobj[2].facePath = "/sdcard/facesort/faces/100/images56346.jpg";
		infoobj[2].classId = 3;
		infoobj[2].photoName = "/sdcard/facesort/faces/100/images56346.jpg";
		infoobj[2].isACK = 0;
		
		infoobj[3] = new facesortInfo();
		infoobj[3].facePath = "/sdcard/facesort/faces/100/images75464.jpg";
		infoobj[3].classId = 3;
		infoobj[3].photoName = "/sdcard/facesort/faces/100/images75464.jpg";
		infoobj[3].isACK = 0;
		
		infoobj[4] = new facesortInfo();
		infoobj[4].facePath = "/sdcard/facesort/faces/100/images65.jpg";
		infoobj[4].classId = 3;
		infoobj[4].photoName = "/sdcard/facesort/faces/100/images65.jpg";
		infoobj[4].isACK = 0;
		return infoobj;
	}
    public class ImageAdapter extends BaseAdapter 
    {
        public ImageAdapter(Context c) {
            mContext = c;
        }

        public int getCount() {
            return mFilelist.length;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) 
        {
            ImageView imageView;
            if (convertView == null) 
            {
                imageView = new ImageView(mContext);
                imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
                imageView.setAdjustViewBounds(false);
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(8, 8, 8, 8);
            } 
            else 
            {
                imageView = (ImageView) convertView;
            }
            imageView.setImageBitmap(BitmapFactory.decodeFile(mFilelist[position]));
            
            return imageView;
        }
        private Context mContext;
    }

	private Button start;
	private Button checkResult;

	private String path;
	private String[] mFilelist;
	private ArrayList<String> resultlist = new ArrayList<String>();
	private ArrayList<Integer> clslist = new ArrayList<Integer>();
	private String mInputpath;
	private String mOutputpath;

	private Thread mFacesortThread;
	private Dialog mDialog;
	private Handler mHandler;
}
